package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsModel;
import cn.ctodb.cms.repository.CmsModelRepository;
import cn.ctodb.cms.repository.search.CmsModelSearchRepository;
import cn.ctodb.cms.service.dto.CmsModelDTO;
import cn.ctodb.cms.service.mapper.CmsModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsModel.
 */
@Service
@Transactional
public class CmsModelService {

    private final Logger log = LoggerFactory.getLogger(CmsModelService.class);
    
    @Inject
    private CmsModelRepository cmsModelRepository;

    @Inject
    private CmsModelMapper cmsModelMapper;

    @Inject
    private CmsModelSearchRepository cmsModelSearchRepository;

    /**
     * Save a cmsModel.
     *
     * @param cmsModelDTO the entity to save
     * @return the persisted entity
     */
    public CmsModelDTO save(CmsModelDTO cmsModelDTO) {
        log.debug("Request to save CmsModel : {}", cmsModelDTO);
        CmsModel cmsModel = cmsModelMapper.cmsModelDTOToCmsModel(cmsModelDTO);
        cmsModel = cmsModelRepository.save(cmsModel);
        CmsModelDTO result = cmsModelMapper.cmsModelToCmsModelDTO(cmsModel);
        cmsModelSearchRepository.save(cmsModel);
        return result;
    }

    /**
     *  Get all the cmsModels.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsModelDTO> findAll() {
        log.debug("Request to get all CmsModels");
        List<CmsModelDTO> result = cmsModelRepository.findAll().stream()
            .map(cmsModelMapper::cmsModelToCmsModelDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsModel by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsModelDTO findOne(Long id) {
        log.debug("Request to get CmsModel : {}", id);
        CmsModel cmsModel = cmsModelRepository.findOne(id);
        CmsModelDTO cmsModelDTO = cmsModelMapper.cmsModelToCmsModelDTO(cmsModel);
        return cmsModelDTO;
    }

    /**
     *  Delete the  cmsModel by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsModel : {}", id);
        cmsModelRepository.delete(id);
        cmsModelSearchRepository.delete(id);
    }

    /**
     * Search for the cmsModel corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsModelDTO> search(String query) {
        log.debug("Request to search CmsModels for query {}", query);
        return StreamSupport
            .stream(cmsModelSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsModelMapper::cmsModelToCmsModelDTO)
            .collect(Collectors.toList());
    }
}
